package com.huike.expanbale;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 二级列表
 */
public class MainActivity extends AppCompatActivity {


    //Model：定义的数据
    private String[] groups = {"中医院站", "联想大厦站"};

    //注意，字符数组不要写成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4，B5"}, {"C1,C2,C3,C4"}}
    private String[][] childs = {{"A1", "A2", "A3", "A4"}, {"A1", "A2", "A3", "B4"}};


    private ExpandableListView el_list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        el_list.setAdapter(new ExpandableAdapter());

        el_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, i+"", Toast.LENGTH_SHORT).show();

                return false;
            }
        });

        el_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                Toast.makeText(MainActivity.this, childs[i][i1], Toast.LENGTH_SHORT).show();
                return true;
            }
        });

    }

    private void initView() {
        el_list = (ExpandableListView) findViewById(R.id.el_list);
    }


    class ExpandableAdapter extends BaseExpandableListAdapter {

        @Override
        public int getGroupCount() {
            return groups.length;
        }

        @Override
        public int getChildrenCount(int i) {
            return childs.length;
        }

        @Override
        public Object getGroup(int i) {
            return null;
        }

        @Override
        public Object getChild(int i, int i1) {
            return null;
        }

        @Override
        public long getGroupId(int i) {
            return 0;
        }

        @Override
        public long getChildId(int i, int i1) {
            return 0;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (view == null) {
                view = View.inflate(MainActivity.this, R.layout.father_view, null);
                holder = new ViewHolder(view);
                holder.iv_switch = view.findViewById(R.id.iv_switch);
                holder.tv_bus_way = view.findViewById(R.id.tv_bus_way);

                view.setTag(holder);

            } else {
                holder = (ViewHolder) view.getTag();
            }

            if (b) {
                holder.iv_switch.setImageResource(R.drawable.open);
            } else {
                holder.iv_switch.setImageResource(R.drawable.close);

            }


            return view;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {

            ViewHolder holder  = null;

            if (view == null) {
                view = View.inflate(MainActivity.this, R.layout.childer_view, null);
                holder = new ViewHolder(view);
                holder.tv_bus_way = view.findViewById(R.id.tv_bus_way);
                holder.tv_bus_distance = view.findViewById(R.id.tv_bus_distance);
                holder.tv_bus_time = view.findViewById(R.id.tv_bus_time);
                view.setTag(holder);

            } else {
                holder = (ViewHolder) view.getTag();
            }

            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }

    }

    static class ViewHolder {
        public View rootView;
        public ImageView iv_switch;
        public TextView tv_bus_way;
        public TextView tv_bus_time;
        public TextView tv_bus_distance;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.iv_switch = (ImageView) rootView.findViewById(R.id.iv_switch);
            this.tv_bus_way = (TextView) rootView.findViewById(R.id.tv_bus_way);
            this.tv_bus_time = (TextView) rootView.findViewById(R.id.tv_bus_time);
            this.tv_bus_distance = (TextView) rootView.findViewById(R.id.tv_bus_distance);
        }

    }
}
